package cn.exempale.mscapplication.utils;

import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * 时间工具类
 */
public final class DateUtils {

    private static final String TAG = "DateUtils";

    public static final String DATE_PATTERN = "yyyy-MM-dd";

    public static final String TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static final String MILLISECOND_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS";

    public static final String TIME_SIMPLE_PATTERN = "yyyy-MM-dd HH:mm";


    /**
     * 按照指定格式转换时间
     *
     * @param arg0
     * @param pattern
     * @return
     */
    public static Date parse(String arg0, String pattern) {
        if (arg0 != null && StringUtils.isMeaningful(pattern)) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat(pattern);
                return sdf.parse(arg0);
            } catch (Exception e) {
                Log.e(TAG, "parse: ", e);
            }
        }
        return null;
    }

    /**
     * 按照指定格式转换为字符串
     *
     * @param arg0
     * @param pattern
     * @return
     */
    public static String format(Date arg0, String pattern) {
        if (arg0 != null && StringUtils.isMeaningful(pattern)) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat(pattern);
                return sdf.format(arg0);
            } catch (Exception e) {
                Log.e(TAG, "format: ", e);
            }
        }
        return null;
    }

    /**
     * 按照指定格式转换为字符串
     *
     * @param arg0
     * @param pattern
     * @return
     */
    public static String format(Long arg0, String pattern) {
        if (arg0 != null && StringUtils.isMeaningful(pattern)) {
            return format(new Date(arg0), pattern);
        }
        return null;
    }

    /**
     * 获取两个时间的间隔天数
     *
     * @param arg0
     * @param arg1
     * @return
     */
    public static Integer intervalDays(Date arg0, Date arg1) {
        if (arg0 != null && arg1 != null) {
            Long second = Math.abs(arg0.getTime() - arg1.getTime());
            Long days = second / (1000 * 60 * 60 * 24);
            return days.intValue();
        }
        return null;
    }

    /**
     * 对现在时间增加毫秒值
     *
     * @param millisecond
     * @return
     */
    public static Date add(Long millisecond) {
        if (millisecond != null && millisecond.compareTo(0L) == 1) {
            return add(new Date(), millisecond);
        }
        return null;
    }

    /**
     * 对指定时间增加毫秒值
     *
     * @param millisecond
     * @return
     */
    public static Date add(Date date, Long millisecond) {
        if (date != null && millisecond != null && millisecond.compareTo(0L) == 1) {
            return new Date(date.getTime() + millisecond);
        }
        return null;
    }

    /**
     * 对现在时间减少毫秒值
     *
     * @param millisecond
     * @return
     */
    public static Date subtract(Long millisecond) {
        if (millisecond != null && millisecond.compareTo(0L) == 1) {
            return subtract(new Date(), millisecond);
        }
        return null;
    }

    /**
     * 对指定时间减少毫秒值
     *
     * @param millisecond
     * @return
     */
    public static Date subtract(Date date, Long millisecond) {
        if (date != null && millisecond != null && millisecond.compareTo(0L) == 1) {
            return new Date(date.getTime() - millisecond);
        }
        return null;
    }

    /**
     * 分转毫秒
     *
     * @param minute
     * @return
     */
    public static Long minuteToMillisecond(Integer minute) {
        if (minute != null) {
            return (long) minute * 60 * 1000;
        }
        return null;
    }

    /**
     * 获得指定日期的年份
     *
     * @return
     */
    public static int YEAR(Date date) {
        return getCalendar(date).get(Calendar.YEAR);
    }

    /**
     * 获得指定日期的月份
     *
     * @return
     */
    public static int MONTH(Date date) {
        return getCalendar(date).get(Calendar.MONTH) + 1;
    }

    /**
     * 获得月中天
     *
     * @return
     */
    public static int DAY_M(Date date) {
        return getCalendar(date).get(Calendar.DAY_OF_MONTH) + 1;
    }

    /**
     * 获得周几
     *
     * @return
     */
    public static int DAY_W(Date date) {
        Integer day = getCalendar(date).get(Calendar.DAY_OF_WEEK);
        return day < 3 ? day + 5 : day - 2;
    }

    /**
     * 获得小时
     *
     * @return
     */
    public static int HOUR(Date date) {
        return getCalendar(date).get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 获得分钟
     *
     * @return
     */
    public static int MINUTE(Date date) {
        return getCalendar(date).get(Calendar.MINUTE);
    }

    /**
     * 获得秒
     *
     * @return
     */
    public static int SECOND(Date date) {
        return getCalendar(date).get(Calendar.SECOND);
    }

    /**
     * 获得毫秒
     *
     * @return
     */
    public static int MILLISECOND(Date date) {
        return getCalendar(date).get(Calendar.MILLISECOND);
    }

    /**
     * 判断两个日期是否为同一天
     *
     * @param a
     * @param b
     * @return
     */
    public static boolean isSameDay(Date a, Date b) {
        return (YEAR(a) == YEAR(b) && MONTH(a) == MONTH(b) && DAY_M(a) == DAY_M(b));
    }

    private static Calendar getCalendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    private DateUtils() {
    }

    /**
     * 获取过期日期
     *
     * @param date
     * @param Y
     * @param M
     * @param D
     * @param h
     * @param m
     * @param s
     * @return
     */
    public static Date getExpireDate(Date date, int Y, int M, int D, int h, int m, int s) {
        Calendar calendar = getCalendar(date);
        calendar.set(YEAR(date) + Y, MONTH(date) + M - 1, DAY_M(date) + D - 1, HOUR(date) + h, MINUTE(date) + m, SECOND(date) + s);
        return calendar.getTime();
    }

    /**
     * 获取日期相隔小时数
     *
     * @param a
     * @param b
     * @return
     */
    public static long intervalHours(Date a, Date b) {
        long diff = Math.abs(a.getTime() - b.getTime()); // 获得两个时间的毫秒时间差异
        long nd = 1000 * 24 * 60 * 60;//每天毫秒数
        long nh = 1000 * 60 * 60;//每小时毫秒数
        return diff % nd / nh;
    }

    /**
     * 获取日期相隔分钟数
     *
     * @param a
     * @param b
     * @return
     */
    public static long intervalMinutes(Date a, Date b) {
        long diff = Math.abs(a.getTime() - b.getTime()); // 获得两个时间的毫秒时间差异
        long nd = 1000 * 24 * 60 * 60;//每天毫秒数
        long nh = 1000 * 60 * 60;//每小时毫秒数
        long nm = 1000 * 60;
        return diff % nd % nh / nm;
    }

    /**
     * 日期转字符串 精确到秒
     *
     * @param date
     * @return
     */
    public static String time(Date date) {
        return format(date, TIME_PATTERN);
    }

    /**
     * 日期转字符串 精确到天
     *
     * @param date
     * @return
     */
    public static String date(Date date) {
        return format(date, DATE_PATTERN);
    }

    /**
     * 字符串转日期 精确到秒
     *
     * @param date
     * @return
     */
    public static Date time(String date) {
        return parse(date, TIME_PATTERN);
    }

    /**
     * 字符串转日期 精确到天
     *
     * @param date
     * @return
     */
    public static Date date(String date) {
        return parse(date, DATE_PATTERN);
    }
}